// components/my-chooseAddress/my-chooseAddress.js
import regeneratorRuntime from '../../utils/runtime/runtime.js'
const mapSearch = require(`../../utils/mapSearch.js`);
const fetch = require(`../../utils/fetch.js`);
const getLocation = require(`../../utils/getLocation.js`);
const config = require('../../config.js');
const Promisify = require('../../utils/util')//具体引入路径根据具体情况
const request = Promisify.wxPromisify(wx.request);//ajax请求
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    pois: "",
    totalCityName: "",
    city: '',
    lat: 0,
    lng: 0
  },
  /**
   * 生命周期
   */
  // lifetimes: {
  //   ready: function(options) {
  //     console.log(options);
  //     this.onLoadxxx(options || {})
  //   }
  // },

  /**
   * 组件的方法列表
   */
  methods: {
    async onLoad(options) {
      let that = this;
      let addressType = options.addressType;
      that.setData({
        longitude: options.longitude,
        latitude: options.latitude,
        city: options.city
      })
      const res = await that.getLocationByCityInfo(options.city)
      that.setData({
        lat: res.lat, 
        lng: res.lng
      })
      // 获取地址
      fetch.showAddress(res.lat, res.lng, (newWhere, pois) => {
        if(pois.length > 0) {
          let nowCity = pois[0].ad_info.city
          let cityRex = /县|市/g;
          //去掉县和市
          if (cityRex.test(nowCity)) {
            nowCity = nowCity.substring(0, nowCity.length - 1);
          }
          that.setData({
            pois: pois,
            nowCity: nowCity,
            totalCityName: pois[0].ad_info.city
          })
        }
      });
    },
    // 根据城市名称查询城市详情
    async getLocationByCityInfo(city) {
      let mapApiUrl = `https://apis.map.qq.com/ws/geocoder/v1/`;
      const res = await request({
        url: mapApiUrl,
        data: {
          address: city,
          key: config.mapSecret
        },
        method: 'GET',
      })
      if (res.statusCode == 200 && res.data.status == 0) {
            const {location} = res.data.result
            return location
      }
      return null
    },
    /**
     * 监听input框输入
     */
    listenInput(e) {
      let that = this;
      if (e.detail.value != ``) {
        this.setData({
          clear: true
        })
        mapSearch.showSearchPois(that.data.totalCityName, e.detail.value, `交叉口`, (pois) => {
          that.setData({
            pois: pois
          })

        })
      } else {
        this.setData({
          clear: false
        })
        // 获取地址
        fetch.showAddress(that.data.lat, that.data.lng, (newWhere, pois) => {
          that.setData({
            pois: pois,
          })
        });
      }
    },
    /**
     * 选择地址
     */
    chooseAddress(e) {
      const that = this;
      let latitude = e.currentTarget.dataset.lat;
      let longitude = e.currentTarget.dataset.lng;
      let title = e.currentTarget.dataset.title;
      let pages = getCurrentPages()
      let prevPage = pages[pages.length - 2]
      let mapAddr = {
        latitude,
        longitude,
        title
      }
      
      prevPage.setData({mapAddr})

      console.log(e.currentTarget.dataset);
    
      wx.navigateBack({
        delta: 1,
        success: function () {
            // 获取地址
          fetch.showAddress(latitude, longitude, (newWhere, pois) => {
            let nowCity = pois[0].ad_info.city
            let cityRex = /县|市/g;
            //去掉县和市
            if (cityRex.test(nowCity)) {
              nowCity = nowCity.substring(0, nowCity.length - 1);
            }
            for (var i = 0; i < pois.length; i++) {
              if(pois[i].title === title) {
                pois[i].checked = true;
              } else{
                //遍历数组 给数组元素加上隐藏
                pois[i].checked = false;
              }
            }

            prevPage.setData({
              latitude,
              longitude,
              pois,
              nowCity,
              totalCityName: pois[0].ad_info.city
            })
          });

        }
      })
     
    }

  }
})
